package me.xiaosiqi.server_info.interceptor;

import me.xiaosiqi.server_info.comm.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author xiaosiqi
 * @date 2021/6/16 11:34
 */
public class LogInInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if (request.getRequestURI().endsWith("login")||
                request.getRequestURI().endsWith(".css")||
                request.getRequestURI().endsWith(".ttf")||
                request.getRequestURI().endsWith(".js")){
            return true;
        }
        User login = (User) request.getSession().getAttribute("login");
        if (login!=null){
            return true;
        }else {
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }

    }
}